﻿/*
创建人：zhangrj
日期：2022-05-07
描述：设备维护页面js
*/
var page = {
    url: getRootPath() + "/Ashx/ApsHandler/ResourceMaintHandler.ashx", //后台处理url,
    commonUrl: getRootPath() + "/Ashx/CommonHandler.ashx", //通用方法后台路径
    //设备分页参数
    pagetotal: 0,
    pageSize: 10,
    pageNumber: 1,
    resourceId: "", //设备ID
    comboxUrl: getRootPath() + "/Ashx/ApsHandler/ResourceMaintHandler.ashx", //请求地址
    comboxAction: 'GetEmployee', //请求方法名称
    //初始化页面js
    init: function () {
        page.initPageData();
        page.bind();
        NewcustomizedEasyuiPagination('pp'); //定制化分页
        $('#pp').find('.pageSize').val(page.pageSize); //初始化行数
        resourceMaint.init(); //设备保养初始化
        resEmpPage.init(); //设备人员初始化
        page.bindProductLine(); //产线属性初始化 add by longb 2024-04-01
    },
    //绑定页面事件
    bind: function () {
        //设备列表加载
        $("#gdResourceInfo").datagrid({
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                NewcustomizedEasyuiPagination('pp'); //定制化分页
            },
            columns: [[
        { field: 'ck', title: '选择', checkbox: true },
        { field: 'RESOURCENAME', title: '设备编码', width: 150 },
        { field: 'DESCRIPTION', title: '设备名称', width: 150 },
        { field: 'RESOURCEFAMILYNAME', title: '设备型号', width: 100 },
        { field: 'PRODUCTLINENAME', title: '关联产线', width: 100 },
        { field: 'MAKECALENDARNAME', title: '日历', width: 120 },
        { field: 'WORKSHIFTNAME', title: '班次', width: 120 },
        { field: 'PRIORITYNUM', title: '优先级', width: 60 },
        { field: 'ISUSE', title: '是否启用', width: 80 },
        { field: 'ISVIRTUAL', title: '是否虚拟设备', width: 80, formatter: function (value, row, index) {
            if (value === "0") {
                return "否";
            } else {
                return "是";
            }
        }
        },
        { field: 'ISINFINITE', title: '是否无限能力设备', width: 100 },
        { field: 'ISCOMBINEBATCH', title: '允许组批加工', width: 100 },
        { field: 'UPPERLIMIT', title: '组批数量上限', width: 100 },
        { field: 'COMBINERULE', title: '组批限制', width: 100, formatter: function (value, row, index) {
            var str = [];
            if (value != null) {
                $.each(value.split(','), function (index, item) {
                    if (item == "1") { str.push("同子工艺") }
                    if (item == "2") { str.push("同工艺名") }
                    if (item == "3") { str.push("同工步名") }
                    if (item == "4") { str.push("同工艺参数") }
                }
            );
            }
            return str.join(',')
        }
        },
        { field: 'NOTES', title: '备注', width: 200 },
        { field: 'RESOURCEID', hidden: true }
      ]],
            onClickRow: function (rowIndex, rowData) {
                $("#gdResourceInfo").datagrid("uncheckAll");
                $("#gdResourceInfo").datagrid("checkRow", rowIndex);
                page.fillData(rowData);
            }
        });
        //设备列表分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                //NewcustomizedEasyuiPagination('pp'); //定制化分页
                clearMessage();
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.search();
            }
        });

        //绑定分页go按钮
        $('#pageGo').on("click", function () {
            clearMessage();
            var pageNumber = Number($(this).parents('tr').find('.pagination-num').val());
            page.pageSize = $('#pp').find('.pageSize').val() == "" ? 10 : Number($('#pp').find('.pageSize').val());
            page.pageNumber = pageNumber;
            page.search();
        });

        //查询信息
        $("#btnSearch").on("click", function () {
            page.pageNumber = 1;
            page.search();
        });
        //重置查询
        $("#btnReset").on("click", function () {
            clearMessage();
            $("#txtSearchResourceNum").textbox("setValue", ""); //设备编码
            $("#txtSearchResourceName").textbox("setValue", ""); //设备名称
            $("#selSearchResourceFamily").combobox("setValue", ""); //设备型号
            $("#selQueryWorkCenter").combobox("setValue", "全部"); //工区
            $("#selQueryTeam").combo("panel").empty(); //班组
            $("#selQueryTeam").combo("clear")
        });
        //新建设备信息
        $("#btnResourceAdd").on("click", function () {
            clearMessage();
            page.addResourceData();
        });
        //保存设备信息
        $("#btnResourceUpdate").on("click", function () {
            clearMessage();
            page.updateResourceData();
        });
        //打开批量更新弹窗
        $("#btnBatchResUpdate").on("click", function () {
            clearMessage();
            page.showMessage2("", true);
            var rows = $("#gdResourceInfo").datagrid('getChecked'); //获取勾选的行
            if (rows.length === 0) {
                displayMessage("未选择设备", false);
                return;
            }
            //打开弹窗
            $("#dg").dialog({
                onClose: function () { //关闭事件
                    $("#mainDiv").find(".easyui-linkbutton").each(function () {
                        $(this).linkbutton('enable');
                    });
                    page.search();
                }
            });
            //将父页面的所有按钮置为无效
            $("#mainDiv").find(".easyui-linkbutton").each(function () {
                $(this).linkbutton('disable');
            });
        });
        //弹窗保存
        $("#btnBatchSave").on("click", function () {
            clearMessage();
            page.batchUpdateResourceData();
        });
        //弹窗关闭
        $("#btnClose").on("click", function () {
            $("#dg").dialog('close');
        });
        //删除设备信息
        $("#btnResourceDele").on("click", function () {
            clearMessage();
            //            page.deleteResourceData();
        });

        //打开组批参数维护更新弹窗 add by longb 20240724
        $("#btnBatchParams").on("click", function () {
            clearMessage();
            page.showMessage2("statusMessage3", "", true);
            var rows = $("#gdResourceInfo").datagrid('getChecked'); //获取勾选的行
            if (rows.length === 0) {
                displayMessage("未选择设备", false);
                return;
            }
            $('#Form1').form('clear')
            if (rows.length === 1) {
                $("#IsCombineBatch").combobox("setValue", (rows[0].ISCOMBINEBATCH == "是" ? "1" : "0"));
                $("#UpperLimit").numberbox("setValue", rows[0].UPPERLIMIT);
                //$("#CombineRule1").checkbox("check");
                if (rows[0].COMBINERULE != null) {
                    $.each(rows[0].COMBINERULE.split(','), function (index, item) {
                        $('#CombineRule' + item).checkbox("check");
                    });
                }
            }
            //打开弹窗
            $("#DivDg").dialog({
                onClose: function () { //关闭事件
                    $("#mainDiv").find(".easyui-linkbutton").each(function () {
                        $(this).linkbutton('enable');
                    });
                    page.search();
                }
            });
            //将父页面的所有按钮置为无效
            $("#mainDiv").find(".easyui-linkbutton").each(function () {
                $(this).linkbutton('disable');
            });
        });
    },
    //初始化页面数据
    initPageData: function () {
        page.post(page.url, { action: "InitPageData" }, true, function (res) {
            //绑定查询区域设备型号下拉框
            $("#selSearchResourceFamily").combobox({
                valueField: 'RESOURCEFAMILYID',
                textField: 'RESOURCEFAMILYNAME',
                panelHeight: 200,
                data: res.DBDataSet.dtResourceFamily //下拉数据
            });
            //绑定填写区域设备型号下拉框
            $("#selResourceFamily").combobox({
                valueField: 'RESOURCEFAMILYID',
                textField: 'RESOURCEFAMILYNAME',
                panelHeight: 200,
                data: res.DBDataSet.dtResourceFamily //下拉数据
            });
            //绑定日历下拉框
            $("#selMakeCalendar,#selNextYearMakeCalendar").combobox({
                valueField: 'MAKECALENDARID',
                textField: 'MAKECALENDARNAME',
                panelHeight: 200,
                data: res.DBDataSet.dtMakeCalendar //下拉数据
            });
            //绑定班次下拉框
            $("#selWorkShift").combobox({
                valueField: 'WORKSHIFTID',
                textField: 'WORKSHIFTNAME',
                panelHeight: 200,
                data: res.DBDataSet.dtWorkShift //下拉数据
            });
            //绑定工厂下拉框
            $("#selFactory").combobox({
                valueField: 'FACTORYID',
                textField: 'FACTORYNAME',
                panelHeight: 200,
                data: res.DBDataSet.dtFactory, //下拉数据
                onSelect: function (record) {
                    if (record.FACTORYID !== "") {
                        page.bindWorkCenter(record.FACTORYID); //加载工区
                    }
                }
            });
            //绑定查询工区下拉框
            var workCenter = res.DBDataSet.dtWorkCenter;
            workCenter.splice(0, 0, { WORKCENTERNAME: "全部", WORKCENTERID: "全部", NOTES: null, WCTYPE: null });
            workCenter.splice(0, 0, { WORKCENTERNAME: "未维护", WORKCENTERID: "", NOTES: null, WCTYPE: null });
            $("#selQueryWorkCenter").combobox({
                valueField: 'WORKCENTERID',
                textField: 'WORKCENTERNAME',
                panelHeight: 200,
                data: workCenter, //下拉数据
                value: "全部",
                onSelect: function (record) {
                    page.bindQueryTeam(record.WORKCENTERID); //加载查询班组
                }
            });
            // $("#selIsBottLeneck").combobox("setValue", "0"); //瓶颈设备
            // $("#selPriorityNum").combobox("setValue", "1"); //优先级
        });
    },
    //绑定工区
    bindWorkCenter: function (factoryId) {
        page.post(page.url, { action: "GetWorkCenter", factoryId: factoryId }, false, function (res) {
            if (res.Result) {
                //绑定工区下拉框
                $("#selWorkCenter").combobox({
                    valueField: 'WORKCENTERID',
                    textField: 'WORKCENTERNAME',
                    panelHeight: 200,
                    data: res.DBTable, //下拉数据
                    onSelect: function (record) {
                        page.bindTeam(record.WORKCENTERID); //加载班组
                    }
                });
            }
        });
    },
    //绑定班组
    bindTeam: function (workcenterId) {
        page.post(page.commonUrl, { action: "GetTeam", workCenterId: workcenterId }, false, function (res) {
            if (res.Result) {
                //绑定班组
                $("#selTeam").combobox({
                    valueField: 'TEAMID',
                    textField: 'TEAMNAME',
                    panelHeight: 200,
                    data: res.DBTable //下拉数据
                });
            }
        });
    },
    //绑定查询班组
    bindQueryTeam: function (workcenterId) {
        page.post(page.commonUrl, { action: "GetTeam", workCenterId: workcenterId }, false, function (res) {
            if (res.Result) {
                //绑定查询班组
                var team = res.DBTable;
                team[0].TEAMNAME = "未维护";
                team.splice(0, 0, { TEAMID: '全部', TEAMNAME: '全部' });
                $("#selQueryTeam").combobox({
                    valueField: 'TEAMID',
                    textField: 'TEAMNAME',
                    panelHeight: 200,
                    data: team, //下拉数据
                    value: "全部"
                });
            } else {
                $("#selQueryTeam").combobox({
                    valueField: 'TEAMID',
                    textField: 'TEAMNAME',
                    panelHeight: 200,
                    data: [{ TEAMID: '全部', TEAMNAME: '全部' }, { TEAMNAME: "未维护", TEAMID: ""}] //下拉数据
                });
            }
        });
    },
    //绑定产线属性 add by longb 2024-04-01
    bindProductLine: function () {
        page.post(page.url, { action: "getCustomObjectData" }, false, function (res) {
            //绑定班组
            $("#selProductLine").combobox({
                valueField: 'CUSTOMOBJECTITEMSID',
                textField: 'CUSTOMOBJECTITEMSNAME',
                panelHeight: 200,
                data: res //下拉数据
            });
        });
    },

    //查询
    search: function () {
        clearEasyuiDataGrid("gdResourceInfo");
        page.reloadPage();
        var resourceName = $("#txtSearchResourceNum").textbox("getValue"); //设备编码
        var description = $("#txtSearchResourceName").textbox("getValue"); //设备名称
        var resourceFamilyname = $("#selSearchResourceFamily").combobox("getText"); //设备型号
        var workcentername = $("#selQueryWorkCenter").combobox("getText"); //工区
        var teamName = $("#selQueryTeam").combobox("getText"); //班组
        var isinfinite = $("#isinfinite1").combobox("getValue");
        var isNotFactory = $("#isNotFactory").combobox("getValue"); //无分厂设备
        var formData = {
            pageSize: page.pageSize,
            pageNumber: page.pageNumber,
            action: "GetResourceListByPage",
            resourceName: resourceName,
            description: description,
            resourceFamilyname: resourceFamilyname,
            workcentername: workcentername == "全部" ? "" : workcentername,
            teamName: teamName == "全部" ? "" : teamName,
            isinfinite: isinfinite,
            isNotFactory: isNotFactory
        };
        page.post(page.url, formData, true, function (res) {
            page.pagetotal = res.RowCount;
            $("#gdResourceInfo").datagrid('loadData', { total: res.RowCount, rows: res.DBTable });
        });
    },
    //新建设备信息
    addResourceData: function () {
        $('#btnResourceAdd').linkbutton('disable'); //禁用按钮
        var resData = page.vaildData();
        if (resData.result === false) {
            displayMessage2(resData.msg, false, ["btnResourceAdd"]);
            return;
        }
        var formData = { action: "SaveResourceData", type: "create", params: JSON.stringify(resData.data) };
        page.post(page.url, formData, true, function (res) {
            if (res.Result) {
                page.search();
            }
            displayMessage2(res.Message, res.Result, ["btnResourceAdd"]);
        });
    },
    //更新设备信息
    updateResourceData: function () {
        $('#btnResourceUpdate').linkbutton('disable'); //禁用按钮
        var resData = page.vaildData();
        if (resData.result === false) {
            displayMessage2(resData.msg, false, ["btnResourceUpdate"]);
            return;
        }
        var formData = { action: "SaveResourceData", type: "update", params: JSON.stringify(resData.data) };
        page.post(page.url, formData, true, function (res) {
            if (res.Result) {
                page.search();
            }
            displayMessage2(res.Message, res.Result, ["btnResourceUpdate"]);
        });
    },
    //批量更新设备信息
    batchUpdateResourceData: function () {
        var rows = $("#gdResourceInfo").datagrid('getChecked'); //获取勾选的行
        if (rows.length === 0) {
            displayMessage("未选择设备", false);
            return;
        }
        $('#btnBatchSave').linkbutton('disable'); //禁用按钮
        $('#btnClose').linkbutton('disable'); //禁用按钮
        var workShiftId = $("#selWorkShift").combobox("getValue"); //班次
        var makeCalendar = $("#selMakeCalendar").combobox("getValue"); //日历
        var nextMakeCalendar = $("#selNextYearMakeCalendar").combobox("getValue"); //下年日历 add by longb 2023-03-28
        var isBottLeneck = $("#selIsBottLeneck").combobox("getValue"); //瓶颈设备
        var priorityNum = $("#selPriorityNum").combobox("getValue"); //优先级
        var isinfinite = $("#isinfinite").combobox("getValue"); //是否无限能力设备 add by longb 2023-08-23
        var formData = {
            action: "SaveBatchResourceData",
            type: "update",
            workShiftId: workShiftId,
            makeCalendar: makeCalendar,
            nextMakeCalendar: nextMakeCalendar, //add by longb 2023-03-28
            isBottLeneck: isBottLeneck,
            priorityNum: priorityNum,
            isinfinite: isinfinite,
            params: JSON.stringify(rows)
        };
        page.post(page.url, formData, true, function (res) {
            page.showMessage2(res.Message, res.Result);
            $('#btnBatchSave').linkbutton('enable'); //启用按钮
            $('#btnClose').linkbutton('enable'); //启用按钮
        });
    },
    //组批参数维护 add by longb 20240724
    saveBatchParams: function () {
        var IsCombineBatch = $("#IsCombineBatch").combobox("getValue"); //允许组批加工
        var UpperLimit = $("#UpperLimit").numberbox("getValue"); //组批数量上限
        var checkedValues = [];
        $("input[name='CombineRule']:checked").each(function () {
            checkedValues.push($(this).val());
        });
        var rows = $("#gdResourceInfo").datagrid('getChecked'); //获取勾选的行
        var formData = {
            action: "saveResourceBatchParams",
            IsCombineBatch: IsCombineBatch,
            UpperLimit: UpperLimit,
            CombineRule: checkedValues.join(','),
            params: JSON.stringify(rows)
        }
        page.post(page.url, formData, true, function (res) {
            page.showMessages("statusMessage3", res.Message, res.Result);
        });
    },

    //批量删除设备信息
    deleteResourceData: function () {
        $('#btnResourceDele').linkbutton('disable'); //禁用按钮
        var rows = $("#gdResourceInfo").datagrid('getChecked'); //获取勾选的行
        if (rows.length === 0) {
            displayMessage2("未选择设备", false, ["btnResourceDele"]);
            return;
        }
        var formData = { action: "SaveBatchResourceData", type: "delete", params: JSON.stringify(rows) };
        page.post(page.url, formData, true, function (res) {
            if (res.Result) {
                page.search();
            }
            displayMessage2(res.Message, res.Result, ["btnResourceDele"]);
        });
    },
    //post数据（路径，参数，是否异步，成功返回方法）
    post: function (url, param, async, callback) {
        $.ajax({
            type: "Post",
            url: url,
            data: param,
            cashe: false,
            async: async,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
                $("body").find(".easyui-linkbutton").each(function () {
                    $(this).linkbutton('enable');
                });
            }
        });
    },
    //验证填写信息并返回数据
    vaildData: function () {
        var resData = { msg: "", result: true };
        var resourceNum = $("#txtResourceNum").textbox("getValue");
        if (isNullOrWhiteSpaceStr(resourceNum)) {
            resData.result = false;
            resData.msg = "未填写设备编码";
            return resData;
        }
        var resourceName = $("#txtResourceName").textbox("getValue");
        if (isNullOrWhiteSpaceStr(resourceName)) {
            resData.result = false;
            resData.msg = "未填写设备名称";
            return resData;
        }
        var resourceFamily = $("#selResourceFamily").combobox("getText");
        if (isNullOrWhiteSpaceStr(resourceFamily)) {
            resData.result = false;
            resData.msg = "未选择设备型号";
            return resData;
        }
        var factory = $("#selFactory").combobox("getText");
        if (isNullOrWhiteSpaceStr(factory)) {
            resData.result = false;
            resData.msg = "未选择工厂";
            return resData;
        }
        var workCenter = $("#selWorkCenter").combobox("getText");
        var team = $("#selTeam").combobox("getText");
        if (team === "请选择") {
            team = "";
        }
        var isUse = $("#selIsUse").combobox("getValue"); //是否启用
        var notes = $("#txtRemark").textbox("getValue"); //备注
        var productline = $("#selProductLine").combobox("getValue"); //设备产线属性 add by longb 2024-04-01
        var isVirtual = $("#isVirtual").combobox("getValue"); //是否虚拟设备 add by zhangrj 20250711
        resData.data = [{
            resourceNum: resourceNum,
            resourceName: resourceName,
            resourceFamily: resourceFamily,
            factory: factory,
            workCenter: workCenter,
            team: team,
            isUse: isUse,
            notes: notes,
            productline: productline,
            isVirtual: isVirtual
        }];
        return resData;
    },
    //填充数据
    fillData: function (row) {
        clearMessage();
        resourceMaint.showMessage("", true);
        $("#txtResourceNum").textbox("setValue", row.RESOURCENAME); //设备编码
        $("#txtResourceNum").textbox('disable'); //禁用设备编码，不可编辑
        $("#txtResourceName").textbox("setValue", row.DESCRIPTION); //设备名称
        $("#selResourceFamily").combobox("setValue", row.RESOURCEFAMILYID); //设备型号
        $("#selWorkShift").combobox("setValue", row.WORKSHIFTID); //班次
        $("#selFactory").combobox("setValue", row.FACTORYID); //工厂
        $("#selWorkCenter").combobox("setValue", row.WORKCENTERID); //工区
        $("#selTeam").combobox("setValue", row.TEAMID); //班组
        $("#selIsUse").combobox("setText", row.ISUSE); //是否启用
        $("#selMakeCalendar").combobox("setValue", row.MAKECALENDARID); //日历
        $("#selPriorityNum").combobox("setValue", row.PRIORITYNUM); //优先级
        $("#txtRemark").textbox("setValue", row.NOTES); //备注
        $("#selIsBottLeneck").combobox("setValue", row.ISBOTTLENECK); //瓶颈设备
        $("#selProductLine").combobox("setValue", row.PRODUCTLINEID); //设备产线属性 add by longb 2024-04-01
        $("#isVirtual").combobox("setValue", row.ISVIRTUAL); //是否虚拟设备 add by zhangrj 20250711
        page.resourceId = row.RESOURCEID;
        resourceMaint.pageNumber = 1;
        resourceMaint.loadData(); //加载保养信息
        resEmpPage.pageNumber = 1;
        resEmpPage.loadData(); //加载设备人员信息
        resEmpPage.showMessage("", true);
    },
    //恢复页面
    reloadPage: function (query, result, fill, maint) {
        clearMessage();
        clearEasyuiDataGrid("gdMaintInfo");
        clearEasyuiDataGrid("gdResAndEmp");
        page.resourceId = "";
        resourceMaint.reloadPage(); //重置保养填写数据
        resourceMaint.showMessage("", true);
        $("#txtResourceNum").textbox('enable'); //启用设备编码，可编辑
        $("#txtResourceNum").textbox("setValue", ""); //设备编码
        $("#txtResourceName").textbox("setValue", ""); //设备名称
        $("#selResourceFamily").combobox("setValue", ""); //设备型号
        $("#selWorkShift").combobox("setValue", ""); //班次
        $("#selFactory").combobox("setValue", ""); //工厂
        $("#selWorkCenter").combo("clear"); //工区
        $("#selTeam").combo("clear"); //班组
        $("#selWorkCenter").combo("panel").empty();
        $("#selTeam").combo("panel").empty();
        $("#selMakeCalendar").combobox("setValue", ""); //日历
        $("#selPriorityNum").combobox("setValue", "1"); //优先级
        $("#selIsUse").combobox("setValue", "0"); //是否启用
        $("#isVirtual").combobox("setValue", "0"); //是否虚拟设备 add by zhangrj 20250711
        $("#selIsBottLeneck").combobox("setValue", "0"); //瓶颈设备
        $("#txtRemark").textbox("setValue", ""); //备注
        $("#selProductLine").combobox("setValue", ""); //设备产线属性 add by longb 2024-04-01
    },
    //提示信息(弹窗)
    showMessage2: function (msg, isTrue) {
        if (isTrue) {
            $("#statusMessage2").html("<span>消息：" + msg + "<span>");
        }
        else {
            $("#statusMessage2").html("<span style='color:red'>消息：" + msg + "<span>");
        }
    },
    showMessages: function (id, msg, isTrue) {
        if (isTrue) {
            $("#" + id).html("<span>消息：" + msg + "<span>");
        }
        else {
            $("#" + id).html("<span style='color:red'>消息：" + msg + "<span>");
        }
    }
};
//设备保养对象
var resourceMaint = {
    //保养分页参数
    pagetotal: 0,
    pageSize: 10,
    pageNumber: 1,
    //初始化
    init: function () {
        resourceMaint.bind();
    },
    //绑定页面事件
    bind: function () {
        customizedEasyuiPagination('pp1'); //定制化分页
        //设备保养列表加载
        $("#gdMaintInfo").datagrid({
            onLoadSuccess: function (data) {
                $('#pp1').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: resourceMaint.pagetotal,
                    pageSize: resourceMaint.pageSize,
                    pageNumber: resourceMaint.pageNumber
                });
                customizedEasyuiPagination('pp1'); //定制化分页
            },
            columns: [[
        { field: 'ck', title: '选择', checkbox: true },
        { field: 'RESOURCENAME', title: '设备编码', width: 180 },
        {
            field: 'UPKEEPSTARTDATE', title: '开始时间', width: 150, formatter: function (value, row, index) {
                return dayjs(value).format('YYYY-MM-DD HH:mm:ss');
            }
        },
        {
            field: 'UPKEEPENDDATE', title: '结束时间', width: 150, formatter: function (value, row, index) {
                return dayjs(value).format("YYYY-MM-DD HH:mm:ss");
            }
        },
        { field: 'RESOURCEUPKEEPINFOID', hidden: true }
      ]]
        });
        //设备保养列表分页事件
        $('#pp1').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp1'); //定制化分页
                resourceMaint.pageSize = pageSize; resourceMaint.pageNumber = pageNumber;
                resourceMaint.loadData();
            }
        });
        $(document).ready(function () {
            //绑定分页go按钮
            $('#pageGo1').on("click", function () {
                resourceMaint.pageNumber = $(this).parents('tr').find('.pagination-num').val();
                resourceMaint.loadData();
            });
        });
        //添加保养
        $("#btnMaintAdd").on("click", function () {
            clearMessage();
            resourceMaint.addMaint();
        });
        //删除保养
        $("#btnMaintDele").on("click", function () {
            clearMessage();
            resourceMaint.deleMaint();
        });
    },
    //加载设备保养数据
    loadData: function () {
        resourceMaint.reloadPage();
        if (page.resourceId === "") {
            resourceMaint.showMessage("没有选择设备", false);
            return;
        }
        var formData = {
            action: "GetResourceMaintInfo",
            resourceId: page.resourceId,
            pageSize: resourceMaint.pageSize,
            pageNumber: resourceMaint.pageNumber
        };
        page.post(page.url, formData, true, function (res) {
            resourceMaint.pagetotal = res.RowCount;
            $("#gdMaintInfo").datagrid('loadData', { total: res.RowCount, rows: res.DBTable });
        });
    },
    //添加设备保养信息
    addMaint: function () {
        $('#btnMaintAdd').linkbutton('disable'); //禁用按钮
        if (isNullOrWhiteSpaceStr(page.resourceId)) {
            resourceMaint.showMessage("未选择设备", false);
            $("#btnMaintAdd").linkbutton('enable');
            return;
        }
        var startTime = $("#txtStartTime").datetimebox("getValue");
        if (isNullOrWhiteSpaceStr(startTime)) {
            resourceMaint.showMessage("未选择开始时间", false);
            $("#btnMaintAdd").linkbutton('enable');
            return;
        }
        var endTime = $("#txtEndTime").datetimebox("getValue");
        if (isNullOrWhiteSpaceStr(endTime)) {
            resourceMaint.showMessage("未选择结束时间", false);
            $("#btnMaintAdd").linkbutton('enable');
            return;
        }
        if (startTime >= endTime) {
            resourceMaint.showMessage("开始时间不能大于结束时间", false);
            $("#btnMaintAdd").linkbutton('enable');
            return;
        }
        var formData = {
            action: "SaveResourceMaintData",
            startTime: startTime,
            endTime: endTime,
            resourceId: page.resourceId
        };
        page.post(page.url, formData, true, function (res) {
            if (res.Result) {
                resourceMaint.loadData();
            }
            resourceMaint.showMessage(res.Message, res.Result);
            $("#btnMaintAdd").linkbutton('enable');
        });
    },
    //删除设备保养信息
    deleMaint: function () {
        $('#btnMaintDele').linkbutton('disable'); //禁用按钮
        var rows = $("#gdMaintInfo").datagrid('getChecked'); //获取勾选的行
        if (rows.length === 0) {
            resourceMaint.showMessage("未选择保养信息", false);
            $("#btnMaintDele").linkbutton('enable');
            return;
        }
        var formData = {
            action: "DeleResourceMaintData",
            params: JSON.stringify(rows)
        };
        page.post(page.url, formData, true, function (res) {
            if (res.Result) {
                resourceMaint.loadData();
            }
            resourceMaint.showMessage(res.Message, res.Result);
            $("#btnMaintDele").linkbutton('enable');
        });
    },
    //重置保养填写控件
    reloadPage: function () {
        resourceMaint.showMessage("", true);
        $("#txtStartTime").datetimebox("setValue", "");
        $("#txtEndTime").datetimebox("setValue", "");
    },
    //提示信息
    showMessage: function (msg, isTrue) {
        if (isTrue) {
            if (msg === "") {
                $("#statusMessage1").html("");
            } else {
                $("#statusMessage1").html("<span>消息：" + msg + "<span>");
            }
        }
        else {
            $("#statusMessage1").html("<span style='color:red'>消息：" + msg + "<span>");
        }
    }
};
//设备人员对象
var resEmpPage = {
    //分页参数
    pagetotal: 0,
    pageSize: 10,
    pageNumber: 1,
    //初始化
    init: function () {
        resEmpPage.bind();
    },
    //绑定页面事件
    bind: function () {
        customizedEasyuiPagination('pp2'); //定制化分页
        //设备已绑定人员表格
        $("#gdResAndEmp").datagrid({
            onLoadSuccess: function (data) {
                $('#pp2').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: resEmpPage.pagetotal,
                    pageSize: resEmpPage.pageSize,
                    pageNumber: resEmpPage.pageNumber
                });
                customizedEasyuiPagination('pp2'); //定制化分页
            },
            columns: [[
        { field: 'ck', title: '选择', checkbox: true },
        { field: 'EMPLOYEEID', title: '人员id', hidden: true, width: 150 },
        { field: 'EMPLOYEENAME', title: '人员编号', width: 150 },
        { field: 'FULLNAME', title: '人员姓名', width: 100 },
        { field: 'WORKCENTERNAME', title: '工区', width: 120 },
        { field: 'TEAMNAME', title: '班组', width: 120 }
      ]]
        });
        //设备保养列表分页事件
        $('#pp2').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp2'); //定制化分页
                resEmpPage.pageSize = pageSize; resEmpPage.pageNumber = pageNumber;
                resEmpPage.loadData();
            }
        });
        $(document).ready(function () {
            //绑定分页go按钮
            $('#pageGo2').on("click", function () {
                resEmpPage.pageNumber = $(this).parents('tr').find('.pagination-num').val();
                resEmpPage.loadData();
            });

            $('#EmployeeCombox').selectCombox({
                search: function () { },
                cancel: function () { },
                selected: function (val, text, record) {
                    $('#txtCombox').textbox('setValue', text);
                    $('#divComboxTxt').show();
                    $('#divComboxSelect').hide();
                    $('#divComboxSearch').show();
                    $('#divComboxClear').hide();
                }
            });
            // 添加设备
            $("#btnResAddEmp").on('click', function () {
                var datas = $("#gdResourceInfo").datagrid('getChecked');
                var selectEmp = $('#txtCombox').selectCombox('getValue');
                if (!selectEmp) {
                    resEmpPage.showMessage('请选择要添加的人员', false);
                    return;
                }
                if (datas.length === 0) {
                    resEmpPage.showMessage('请选择设备', false);
                    return;
                }
                if (datas.length > 1) {
                    resEmpPage.showMessage('不能同时选择多条设备', false);
                    return;
                }
                resEmpPage.resourceAddEmp();
            });

            // 移除设备
            $("#btnResRemoveEmp").on('click', function () {
                var ckResourceList = $("#gdResAndEmp").datagrid('getChecked');
                if (ckResourceList.length === 0) {
                    resEmpPage.showMessage('请选择待移除的设备人员', false);
                    return;
                }
                resEmpPage.resourceRemoveEmp();
            });
            //异常纠正
            $("#btnCorrect").on('click', function () {
                resEmpPage.dealAbnormal();
            });
        });
    },
    // 查询设备已绑定人员信息
    loadData: function () {
        var formData = {
            action: "GetResAndEmpInfo",
            resourceId: page.resourceId,
            pageSize: resEmpPage.pageSize,
            pageNumber: resEmpPage.pageNumber
        };
        page.post(page.url, formData, true, function (res) {
            resEmpPage.pagetotal = res.RowCount;
            $("#gdResAndEmp").datagrid('loadData', { total: res.RowCount, rows: res.DBTable });
        });
    },
    // 设备添加人员
    resourceAddEmp: function () {
        var datas = $("#gdResourceInfo").datagrid('getChecked');
        resourceid = datas[0].RESOURCEID;
        resourcename = datas[0].RESOURCENAME;
        employeeid = $('#txtCombox').selectCombox('getValue');

        var formData = {
            action: "ResourceAddEmployee",
            resourceid: resourceid,
            resourcename: resourcename,
            employeeid: employeeid
        };
        page.post(page.url, formData, true, function (res) {
            if (res.Result) {
                resEmpPage.showMessage('添加成功', res.Result);
            } else {
                resEmpPage.showMessage(res.Message, res.Result);
            }
            resEmpPage.loadData();
        });
    },
    // 设备移除人员
    resourceRemoveEmp: function () {
        var ckResourceList = $("#gdResourceInfo").datagrid('getChecked');
        resourceid = ckResourceList[0].RESOURCEID;
        resourcename = ckResourceList[0].RESOURCENAME;

        var formData = {
            action: "ResourceRemoveEmployee",
            resourceid: resourceid,
            resourcename: resourcename,
            employeeList: JSON.stringify($("#gdResAndEmp").datagrid('getChecked'))
        };
        page.post(page.url, formData, true, function (res) {
            if (res.Result) {
                resEmpPage.showMessage('移除成功', res.Result);
            } else {
                resEmpPage.showMessage(res.Message, res.Result);
            }
            resEmpPage.loadData();
        });
    },
    // 处理异常人员
    dealAbnormal: function () {
        employeeid = $('#txtCombox').selectCombox('getValue');
        if (!employeeid) {
            resEmpPage.showMessage('请选择人员', false);
            return;
        }
        var formData = {
            action: "DealAbnormalEmp",
            employeeid: employeeid
        };
        page.post(page.url, formData, true, function (res) {
            resEmpPage.showMessage(res.Message, res.Result);
        });
    },
    showMessage: function (msg, isTrue) {
        if (isTrue) {
            if (msg === "") {
                $("#statusRes").html("");
            } else {
                $("#statusRes").html("<span>消息：" + msg + "<span>");
            }
        }
        else {
            $("#statusRes").html("<span style='color:red'>消息：" + msg + "<span>");
        }
    }
};

$(function () {
    page.init();
})